package org.mzy.org.controller;

import io.swagger.annotations.ApiOperation;
import org.mzy.basic.anno.PreAuthorize;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.ExcelUtil;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.Shop;
import org.mzy.org.domain.ShopAuditLog;
import org.mzy.org.query.ShopQuery;
import org.mzy.org.service.IShopService;
import org.mzy.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     *
     * @param shop 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "添加/修改店铺", sn = "shop:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @PreAuthorize(name = "删除店铺", sn = "shop:delete")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询店铺", sn = "shop:findById")
    public Shop findById(@PathVariable("id") Long id) {
        return shopService.findById(id);
    }

    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    @PreAuthorize(name = "查询所有店铺信息", sn = "shop:list")
    public List<Shop> list() {
        return shopService.findAll();
    }

    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name = "分页查询店铺信息", sn = "shop:json")
    public PageList<Shop> json(@RequestBody ShopQuery query) {
        return shopService.queryByPage(query);
    }

    // 店铺入住接口
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
//        try {
        shopService.settlement(shop);
        System.out.println("66666666666666666666666666666666666");
        return AjaxResult.me().success();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙，请稍后再试");
//        }
    }

    /*店铺二次提交*/
    @PostMapping("/settlement/again")
    public AjaxResult again(@RequestBody @Validated ShopVo shopVo) {
        shopService.again(shopVo);
        return AjaxResult.me().success();
    }

    /**
     * 店铺入驻审核
     */
    @GetMapping("/active/{id}")
    @PreAuthorize(name = "店铺入住审核", sn = "shop:active")
    public AjaxResult active(@PathVariable("id") Long id) throws MessagingException {
        shopService.active(id);
        return AjaxResult.me().success();
    }

    /**
     * 导出
     */
    @GetMapping("/export/excel")
    @PreAuthorize(name = "店铺信息导出", sn = "shop:exportExcel")
    public void exportExcel(HttpServletResponse res) {
        List<Shop> all = shopService.findAll();
        System.out.println(all);
        ExcelUtil.exportExcel(all, "", "sheet1", Shop.class, "店铺列表.xlsx",
                res);
    }

    /**
     * 导入
     */
    @PostMapping("/import/excel")
    @PreAuthorize(name = "店铺信息导入", sn = "shop:importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 2, Shop.class);
        shopService.batchAdd(shops);
        shops.forEach(System.out::println);
    }

    /**
     * 导入
     */
    @GetMapping("/echarts")
    @PreAuthorize(name = "加载柱状图", sn = "shop:loadShopEchartsData")
    public Map<String, Object> loadShopEchartsData() {
        return shopService.loadShopEchartsData();
    }

    // 批量删除
    @PatchMapping
    @ApiOperation("批量删除")
    @PreAuthorize(name = "批量删除店铺", sn = "shop:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        try {
            shopService.batchDel(ids);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    /**
     * 店铺审核通过
     * /audit/pass
     */
    @PostMapping("/audit/pass")
    @PreAuthorize(name = "店铺审核通过", sn = "shop:auditPass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }

    /**
     * 店铺审核驳回
     * /audit/pass
     */
    @PostMapping("/audit/reject")
    @PreAuthorize(name = "店铺审核驳回", sn = "shop:auditReject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail(e.getMessage());
        }
    }

    /**
     * 店铺激活
     */
    @GetMapping("/activite/{id}")
    @PreAuthorize(name = "店铺激活", sn = "shop:activite")
    public void activite(@PathVariable("id") Long id) {
        try {
            shopService.activite(id);
//            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
//            return AjaxResult.me().fail(e.getMessage());
        }
    }
}
